package com.linkai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkai.dto.admin.business.InputOrder4UpdateDto;
import com.linkai.dto.admin.business.InputOrderUpdateDto;
import com.linkai.dto.admin.business.inputOrderReback.ProductOfUnConnectTableDto;
import com.linkai.dto.admin.business.inputOrderReback.RebackIndexDto;
import com.linkai.dto.admin.business.inputOrderReback.UnConnectedSaveDto;
import com.linkai.entity.*;
import com.linkai.mapper.*;
import com.linkai.service.InputRebackOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yamon
 * @since 2021-02-26
 */
@Service
public class InputRebackOrderServiceImpl extends ServiceImpl<InputRebackOrderMapper, InputRebackOrder> implements InputRebackOrderService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private BstoreMapper bstoreMapper;
    @Autowired
    private MoneyaccountMapper moneyaccountMapper;
    @Autowired
    private InputRebackOrderProductMapper inputRebackOrderProductMapper;

    @Autowired
    private BproductMapper bproductMapper;

    @Autowired
    private SupplierMapper supplierMapper;
    @Override
    public List<RebackIndexDto> listAllRebackOrder() {
        final List<InputRebackOrder> rebackOrders = this.list();
        List<RebackIndexDto> list = new ArrayList<>();
        for (InputRebackOrder rebackOrder : rebackOrders) {
            final RebackIndexDto rebackIndexDto = new RebackIndexDto();
            rebackIndexDto.setId(rebackOrder.getId());
            rebackIndexDto.setInputOrderNumber(rebackOrder.getInputOrderNumber());
            rebackIndexDto.setInpDate(rebackOrder.getInpDate());
            rebackIndexDto.setPayer(moneyaccountMapper.selectById(rebackOrder.getAccId()).getAccName());
            rebackIndexDto.setEmployeeName(employeeMapper.selectById(rebackOrder.getEmpId()).getName());
            rebackIndexDto.setStoreName(bstoreMapper.selectById(rebackOrder.getBstoId()).getBstoTitle());
            rebackIndexDto.setPrice(rebackOrder.getPrice());
            rebackIndexDto.setStatus(rebackOrder.getInpState());
            list.add(rebackIndexDto);
        }
        return list;
    }

    /**
     * 获取一个非关联的dto
     *
     *
     * @param rebackOrder
     * @param id
     * @return
     */
    @Override
    public UnConnectedSaveDto getOneUnConnectedDto(InputRebackOrder rebackOrder, Integer id) {
        final UnConnectedSaveDto unConnectedSaveDto = new UnConnectedSaveDto();
        unConnectedSaveDto.setOrderId(rebackOrder.getInputOrderNumber());
        unConnectedSaveDto.setInputDate(rebackOrder.getInpDate());
        unConnectedSaveDto.setEmployeeName(employeeMapper.selectById(rebackOrder.getEmpId()).getName());
        unConnectedSaveDto.setStoreName(bstoreMapper.selectById(rebackOrder.getBstoId()).getBstoTitle());
        unConnectedSaveDto.setPrice(rebackOrder.getPrice());
        unConnectedSaveDto.setAccountName(moneyaccountMapper.selectById(rebackOrder.getAccId()).getAccName());
        unConnectedSaveDto.setBack(rebackOrder.getBack());
        //设置商品list
        List<ProductOfUnConnectTableDto> products = new ArrayList<>();
        //根据id，查到所有的商品
        final QueryWrapper<InputRebackOrderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("input_reback_order_id",id);
        final List<InputRebackOrderProduct> inputRebackOrderProducts = inputRebackOrderProductMapper.selectList(queryWrapper);
        for (InputRebackOrderProduct rebackOrderProduct : inputRebackOrderProducts) {
            final ProductOfUnConnectTableDto unConnectTableDto = new ProductOfUnConnectTableDto();
            final Bproduct bproduct = bproductMapper.selectById(rebackOrderProduct.getProductId());
            final Supplier supplier = supplierMapper.selectById(rebackOrderProduct.getSupId());
            //storage的商品Id
            unConnectTableDto.setId(rebackOrder.getId());
            unConnectTableDto.setProId(rebackOrderProduct.getProductId());
            unConnectTableDto.setProTitle(bproduct.getBproTitle());
            unConnectTableDto.setProUnit(bproduct.getBproUnit());
            unConnectTableDto.setSupName(supplier.getSupName());
            unConnectTableDto.setProPrice(rebackOrderProduct.getInpordPrice());
            unConnectTableDto.setProNum(rebackOrderProduct.getInpordNum());
            unConnectTableDto.setProMoney(rebackOrderProduct.getEachSum());
            products.add(unConnectTableDto);
        }
        unConnectedSaveDto.setProducts(products);
        return unConnectedSaveDto;
    }

    /**
     * 显示一个关联进货单的退货单信息
     *
     * @param id id
     * @return
     */
    @Override
    public InputOrderUpdateDto showRebackInfo(Integer id) {
        //封装信息
        final InputRebackOrder inputRebackOrder = baseMapper.selectById(id);
        InputOrderUpdateDto inputOrderUpdateDto = new InputOrderUpdateDto();
        inputOrderUpdateDto.setInputOrderNumber(inputRebackOrder.getInputOrderNumber());
        inputOrderUpdateDto.setId(inputRebackOrder.getId());
        inputOrderUpdateDto.setSupplier(supplierMapper.selectById(inputRebackOrder.getSupplier()));
        //查找
        //查找employee信息
        final Integer empId = inputRebackOrder.getEmpId();
        if (empId!=null){
            Employee employee = employeeMapper.selectById(empId);
            inputOrderUpdateDto.setEmployee(employee);
        }
        final Integer bstoId = inputRebackOrder.getBstoId();
        final Bstore bstore = bstoreMapper.selectById(bstoId);
        inputOrderUpdateDto.setBstore(bstore);
        inputOrderUpdateDto.setAccId(moneyaccountMapper.selectById(inputRebackOrder.getAccId()));
        inputOrderUpdateDto.setPrice(inputRebackOrder.getPrice());
        inputOrderUpdateDto.setInpDate(inputRebackOrder.getInpDate());
        inputOrderUpdateDto.setBack(inputRebackOrder.getBack());
        inputOrderUpdateDto.setInpState(inputRebackOrder.getInpState());
        //查找出来所有关联商品信息
        final QueryWrapper<InputRebackOrderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("input_reback_order_id",inputRebackOrder.getInputId());
        List<InputRebackOrderProduct> inputorderProducts = inputRebackOrderProductMapper.selectList(queryWrapper);
        inputorderProducts.forEach(inputorderProduct -> {
            final Bproduct bproduct = bproductMapper.selectById(inputorderProduct.getProductId());
            InputOrder4UpdateDto inputOrder4UpdateDto = new InputOrder4UpdateDto();
            inputOrder4UpdateDto.setBproUnit(bproduct.getBproUnit());
            inputOrder4UpdateDto.setBproTitle(bproduct.getBproTitle());
            inputOrder4UpdateDto.setBproId(inputorderProduct.getProductId());
            inputOrder4UpdateDto.setBproNum(inputorderProduct.getInpordNum());
            inputOrder4UpdateDto.setBinputPrice(inputorderProduct.getInpordPrice());
            inputOrder4UpdateDto.setEachSum(inputorderProduct.getEachSum());
            inputOrderUpdateDto.getBproducts().add(inputOrder4UpdateDto);
        });
        return inputOrderUpdateDto;
    }
}
